package org.example.huadianguanli.controller;

import cn.hutool.json.JSONObject;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.exception.ErrorException;
import org.example.huadianguanli.model.TShopCardModel;
import org.example.huadianguanli.service.TShopCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Date: 2024/2/15 13:57
 */
@RestController
@RequestMapping("/shop-card")
public class TShopCardController extends BaseController<String, TShopCardService, TShopCardModel> {

    @Autowired
    private TShopCardService tShopCardService;
    /**
     * 添加购物车
     * @param params
     * @return
     */
    @PostMapping("/addShopCard")
    public ApiResult addShopCard(@RequestBody Map<String,Object> params) {
        Object shopId = params.get("shopId");
        Object count = params.get("count");

        if (ObjectUtils.isEmpty(shopId) || ObjectUtils.isEmpty(count)){
            throw new ErrorException("购物车信息不能为空！");
        }
        tShopCardService.addShopCard(params);
        return ApiResult.success();
    }

    /**
     * 更新购物车
     * @param params
     * @return
     */
    @PostMapping("/updateShopCard")
    public ApiResult updateShopCard(@RequestBody Map<String,Object> params) {
        Object shopCardId = params.get("shopCardId");
        Object shopCardList = params.get("shopCardList");
        if (org.apache.commons.lang3.ObjectUtils.anyNull(shopCardId,shopCardList)){
            throw new ErrorException("购物车信息不能为空！");
        }
        tShopCardService.updateShopCard(params);
        return ApiResult.success();
    }

    /**
     * 查询当前用户的购物车列表
     * @return
     */
    @GetMapping("/getShopCardList")
    public ApiResult getShopCardList(){
        String userId = getUserId();
        JSONObject result = tShopCardService.getShopCardList(userId);
        return ApiResult.success(result);
    }
}
